import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:xiongan_app_yy/entrys/rp/car_info_list_entity.dart';
import 'package:xiongan_app_yy/pages/common/car_number_page_new.dart';
import 'package:xiongan_app_yy/widgets/comm/input_card_widget_new.dart';

class CarInfoList extends StatefulWidget {
  List<CarInfoListItem> datas;

  CarInfoList(this.datas);

  @override
  _CarInfoListState createState() => _CarInfoListState();
}

class _CarInfoListState extends State<CarInfoList> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      width: double.infinity,
      child: ListView.builder(
        shrinkWrap: true,
        physics: ScrollPhysics(),
        itemBuilder: (context, index) {
          return CarItemInfo(widget.datas.elementAt(index), index, changeItem);
        },
        scrollDirection: Axis.vertical,
        itemCount: widget.datas.length,
        padding: EdgeInsets.only(top: 0),
      ),
    );
  }

  void changeItem(index) {
    setState(() {
      if (index == 0) {
        widget.datas.add(CarInfoListItem());
      } else {
        widget.datas.removeAt(index);
      }
    });
  }
}

class CarItemInfo extends StatefulWidget {
  CarInfoListItem item;
  int index;
  Function changeItem;

  CarItemInfo(this.item, this.index, this.changeItem);

  @override
  _CarItemInfoState createState() => _CarItemInfoState();
}

class _CarItemInfoState extends State<CarItemInfo> {
  TextEditingController controller0 = TextEditingController();
  TextEditingController controller1 = TextEditingController();
  TextEditingController controller2 = TextEditingController();
  String proText = "浙";

  @override
  void initState() {
    if (widget.item.driver != null && widget.item.driver.length > 0) {
      controller0.text = widget.item.driver;
    }
    if (widget.item.phone != null && widget.item.phone.length > 0) {
      controller1.text = widget.item.phone;
    }
    if (widget.item.carno != null && widget.item.carno.length > 0) {
      proText = widget.item.carno.substring(0, 1);
      controller2.text =
          widget.item.carno.substring(1, widget.item.carno.length);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(left: 10, right: 10),
      child: Row(
        children: <Widget>[
          Center(
            child: Text("第${widget.index + 1}位",
                style: TextStyle(color: Colors.blue, fontSize: 14)),
          ),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                InputCardWidgetNew(
                  hintText: "请输入司机姓名",
                  title: "司机姓名:",
                  contro: controller0,
                  onChange: () {
                    widget.item.driver = controller0.text;
                  },
                ),
                InputCardWidgetNew(
                  keyboardType: TextInputType.number,
                  hintText: "请输入司机手机号",
                  title: "司机手机号:",
                  onChange: () {
                    widget.item.phone = controller1.text;
                  },
                  contro: controller1,
                  inputFormatters: [
                    WhitelistingTextInputFormatter(RegExp("[0-9]")),
                    LengthLimitingTextInputFormatter(11),
                  ],
                ),
                CarNumberPageNew(true, controller2, proText,
                    (proText) => _changeText(proText), (value) {
                  widget.item.carno = proText + value;
                }),
              ],
            ),
          ),
          Center(
            child: RaisedButton(
                onPressed: () {
                  widget.changeItem(widget.index);
                },
                color: widget.index == 0 ? Colors.green : Colors.red,
                child: widget.index == 0
                    ? Text(
                        "+ 添加",
                        style: TextStyle(color: Colors.white, fontSize: 14),
                      )
                    : Text("- 删除",
                        style: TextStyle(color: Colors.white, fontSize: 14))),
          )
        ],
      ),
    );
  }

  _changeText(text) {
    proText = text;
  }
}
